/*
 * Copyright (C) 2013 TinhVan Outsourcing.
 */
package com.learning.jquizenglish.network.connection;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

public class NetworkUtility {

    private static final String LOG_TAG = NetworkUtility.class.getSimpleName();

    public static boolean checkStatusNetwork(final Context context) {

        ConnectivityManager conMgr =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conMgr == null) {
            return false;
        }
        NetworkInfo wifiNetwork = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiNetwork != null && (wifiNetwork.isAvailable() && wifiNetwork.isConnectedOrConnecting())) {
            return true;
        }

        NetworkInfo mobileNetwork = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileNetwork != null && (mobileNetwork.isAvailable() && mobileNetwork.isConnectedOrConnecting())) {
            return true;
        }
        
        NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
        if (activeNetwork != null && (activeNetwork.isAvailable() && activeNetwork.isConnectedOrConnecting())) {
            return true;
        }

        State mobile = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
            return true;
        }
        return false;
    }
    
    /**
     * Checks for Internet connectivity
     * @return true if Internet is connected else false 
     */
    public static boolean isInternetConnected(Context context) {
        if(context != null){
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo[] netInfo = cm.getAllNetworkInfo();

            for (NetworkInfo networkInfo : netInfo) {

                if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
                    if (networkInfo.isConnected())
                        return true;

                if (networkInfo.getType() == ConnectivityManager.TYPE_WIMAX) //WIMAX means 4G
                    if (networkInfo.isConnected())
                        return true;

                if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                    if (networkInfo.isConnected())
                        return true;


            }

        }

        return false;
    }

    public static boolean isDataAvailable(Context context) {
        ConnectivityManager conxMgr =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo mobileNwInfo = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNwInfo = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        return ((mobileNwInfo == null ? false : mobileNwInfo.isConnected()) || (wifiNwInfo == null
                ? false
                : wifiNwInfo.isConnected()));
    }

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager conxMgr =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo mobileNwInfo = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNwInfo = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        return ((mobileNwInfo == null ? false : mobileNwInfo.isAvailable()) || (wifiNwInfo == null
                ? false
                : wifiNwInfo.isAvailable()));
    }

    public static boolean checkGPSEnable(Context pContext) {
        LocationManager manager = (LocationManager) pContext.getSystemService(Context.LOCATION_SERVICE);

        boolean isNet = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        boolean isGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (isNet) {
            return true;
        }
        return false;
    }
}
